/*
 * Created on 2005-10-21
 *
 */
package cn.com.pxto.commons.pagesupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author Gauss Cheung
 * PageInfo ��ʹ�ã�
 * 		PageInfo ������Ϊ��Ҫ�õ���ҳ��ѯ�ĺ���Ĳ���ʹ�á����ȴ���һ��PageInfo��������currentPageֵ����Ҫ�ĵڼ�ҳ��
 *  ��pageSize(ҳ��С)����������Ϊ����ݵ���ѯ�����У���ѯ���ûὫ�˶��󴫵ݵ�DAOģ���У�ģ���ݲ�ѯ��
 *  ��ͳ�Ƴ��ѯ����¼ͳ����Ȼ�󽫸�ֵд�뵽�˶����totalResultֵ�С�Ȼ�����calculate()����
 *  �����(totalPage)��ҳ���ѯ������ʼ�У�beginResutl)��DAOģ���ʹ��beginResult��
 *  pageSize��ѯ����Ӧ�Ľ��
 *  	ʹ��PageInfo��Ϊ��ѯ����Ĳ�����ã������ڷ��ز�ѯ����ͬʱҲ�޸���PageInfo�����ݣ������ʾ��ҳ��Ϣ������ֱ��
 *  ʹ�ò�ѯ���ú��PageInfo����
 */
public class PageInfo {
	private int pageSize;    
	private int currentPage =1; 
	private int beginResult;
	private int totalResult;
	private int totalPage = 1;
	
	private static Log log = LogFactory.getLog(PageInfo.class);
	
	public PageInfo(){}

	public PageInfo(int pageSize, int currentPage) {
		super();
		this.pageSize = pageSize;
		this.currentPage = currentPage;
	}

	/*
	 * get to fetch data
	 */
    public int getBeginResult() {
        return beginResult;
    }
    /*
     * get to show
     */
    public int getCurrentPage() {
        return currentPage;
    }
    /*
     * set to calculate beginResult and total page
     */
    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }
    /*
     * get to show and to fetch data
     */
    public int getPageSize() {
        return pageSize;
    }
    /*
     * set to calculate beginResult and totalPage
     */
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    /*
     * get to show
     */
    public int getTotalPage() {
        return totalPage;
    }
    /*
     * get to show
     */
    public int getTotalResult() {
        return totalResult;
    }
    /*
     * set to calculate beginResult and totalPage
     */
    public void setTotalResult(int totalResult) {
        this.totalResult = totalResult;
    }
    
    public void calculate(){
        //calculate totalPage
        totalPage = totalResult / pageSize;
        if (totalResult % pageSize > 0 ) totalPage++;
        
        //set the currentPage
        if (currentPage<=0){
            log.error("calculate page info occurrent an error,caused by value lower 0!");
        }
        if (currentPage*pageSize>totalResult){
            currentPage = totalPage;
        }

        //calculate the beginResult
        beginResult = (currentPage-1)*pageSize;
        if (beginResult<0) beginResult = 0;
    }

	public void clone(PageInfo source){
    	this.pageSize = source.getPageSize();
    	this.currentPage = source.getCurrentPage();
    	this.beginResult = source.getBeginResult();
    	this.totalResult = source.getTotalResult();
    	this.totalPage = source.getTotalPage();
    }
}
